FUNCTION YBC_SWS_DP_APPROVED.
*"----------------------------------------------------------------------
*"*"本地接口：
*"  IMPORTING
*"     VALUE(AFTCS_VALIDATION) TYPE  /OPT/VIM_AFTCS_VALIDATION
*"     VALUE(CUSTOM_FIELD4) TYPE  /OPT/VIM_1HEAD-CUSTOM_FIELD4
*"  TABLES
*"      DP_LIST STRUCTURE  /OPT/DP_LIST
*"      RETURN STRUCTURE  BAPIRET2
*"----------------------------------------------------------------------
*Copy from FM /OPT/AFTCS_RUN_RULES_CN

  DATA: LV_WI          TYPE SWW_WIID,
        LV_WF_TEMPLATE TYPE /PTGWFI/Z_CONST-ZVALUE,
        LV_DB_TASK     TYPE /PTGWFI/Z_CONST-ZVALUE,
        LV_OBJ_KEY     TYPE SWOTOBJID-OBJKEY,
        LV_RETCODE     TYPE SY-SUBRC.

  DATA: LO_INDEXER TYPE REF TO /OPT/CL_VIM_DOC_PROCESSOR.

  DATA: LT_TASKS     TYPE TABLE OF SWR_TASK WITH HEADER LINE,
        LT_ACTIVE_WF TYPE TABLE OF SWR_WIHDR WITH HEADER LINE,
        LT_CONT      TYPE TABLE OF SWR_CONT WITH HEADER LINE,
        LT_MESS      TYPE TABLE OF SWR_MESSAG WITH HEADER LINE.

  REFRESH: RETURN.


* Get the DP workflow number
  CALL FUNCTION '/PTGWFI/CP_READCONSTANT'
    EXPORTING
      CONSTNAME          = 'MAIN_WF_TEMPLATE'
      PRODUCTCODE        = '005'
    IMPORTING
      CONSTVALUE         = LV_WF_TEMPLATE
    EXCEPTIONS
      CONSTANT_NOT_FOUND = 1
      OTHERS             = 2.
  IF SY-SUBRC <> 0.
    LV_WF_TEMPLATE = 'WS00275269'.
  ENDIF.

* Get the DP dashbaord task id
  CALL FUNCTION '/PTGWFI/CP_READCONSTANT'
    EXPORTING
      CONSTNAME          = 'DP_DASHBOARD_TASK_ID'
      PRODUCTCODE        = '005'
    IMPORTING
      CONSTVALUE         = LV_DB_TASK
    EXCEPTIONS
      CONSTANT_NOT_FOUND = 1
      OTHERS             = 2.
  IF SY-SUBRC <> 0.
    LV_DB_TASK = 'TS00275278'.
  ENDIF.

  LOOP AT DP_LIST.

* Clear variables
    CLEAR: RETURN, LV_OBJ_KEY, LV_WI, LV_OBJ_KEY, LV_RETCODE.
    FREE: LO_INDEXER.
    REFRESH: LT_TASKS, LT_ACTIVE_WF, LT_CONT, LT_MESS.


******************************************************************
* Lock the DP document
******************************************************************

    CALL FUNCTION 'ENQUEUE_/OPT/DOCID'
      EXPORTING
        MANDT          = SY-MANDT
        DOCID          = DP_LIST-DOCID
      EXCEPTIONS
        FOREIGN_LOCK   = 1
        SYSTEM_FAILURE = 2
        OTHERS         = 3.
    IF SY-SUBRC <> 0.

      RETURN-TYPE = 'E'.

      CALL FUNCTION 'BALW_BAPIRETURN_GET2'
        EXPORTING
          TYPE   = RETURN-TYPE
          CL     = SY-MSGID
          NUMBER = SY-MSGNO
          PAR1   = SY-MSGV1
          PAR2   = SY-MSGV2
          PAR3   = SY-MSGV3
          PAR4   = SY-MSGV4
        IMPORTING
          RETURN = RETURN.

      RETURN-MESSAGE_V4 = DP_LIST-DOCID.

      APPEND RETURN.
      CLEAR RETURN.

      CONTINUE.
    ENDIF.

******************************************************************
* Update the workflow container
******************************************************************

* Get the workitem id of the DP workflow
    REFRESH LT_TASKS.
    LT_TASKS-WI_RH_TASK = LV_WF_TEMPLATE.
    APPEND LT_TASKS.

    CLEAR LT_TASKS.

    LV_OBJ_KEY = DP_LIST-DOCID.

    CALL FUNCTION 'SAP_WAPI_WORKITEMS_TO_OBJECT'
      EXPORTING
        OBJTYPE     = '/OPT/V1001'
        OBJKEY      = LV_OBJ_KEY
      IMPORTING
        RETURN_CODE = LV_RETCODE
      TABLES
        TASK_FILTER = LT_TASKS
        WORKLIST    = LT_ACTIVE_WF.

    READ TABLE LT_ACTIVE_WF INDEX 1.
    IF SY-SUBRC NE 0.
      RETURN-TYPE = 'E'.
      RETURN-MESSAGE = TEXT-E12.
      RETURN-MESSAGE_V4 = DP_LIST-DOCID.

      APPEND RETURN.
      CLEAR RETURN.

      CALL FUNCTION 'DEQUEUE_/OPT/DOCID'
        EXPORTING
          MODE_/OPT/VIM_1HEAD = 'E'
          MANDT               = SY-MANDT
          DOCID               = DP_LIST-DOCID.

      CONTINUE.

    ENDIF.

    LV_WI = LT_ACTIVE_WF-WI_ID.

* Read the workflow container
    CALL FUNCTION 'SAP_WAPI_READ_CONTAINER'
      EXPORTING
        WORKITEM_ID      = LV_WI
      IMPORTING
        RETURN_CODE      = LV_RETCODE
      TABLES
        SIMPLE_CONTAINER = LT_CONT
        MESSAGE_LINES    = LT_MESS.

    IF NOT LV_RETCODE IS INITIAL OR
       LT_CONT IS INITIAL.

      RETURN-TYPE = 'E'.
      RETURN-MESSAGE = TEXT-E12.
      RETURN-MESSAGE_V4 = DP_LIST-DOCID.

      APPEND RETURN.
      CLEAR RETURN.

      CALL FUNCTION 'DEQUEUE_/OPT/DOCID'
        EXPORTING
          MODE_/OPT/VIM_1HEAD = 'E'
          MANDT               = SY-MANDT
          DOCID               = DP_LIST-DOCID.

      CONTINUE.

    ENDIF.

    LOOP AT LT_CONT.
      IF LT_CONT-ELEMENT EQ 'FLOWID'.
        LT_CONT-VALUE = 'CHECKRULES'.
        MODIFY LT_CONT.
      ELSE.
        DELETE LT_CONT.
      ENDIF.
    ENDLOOP.

* Modify the workflow container
    CALL FUNCTION 'SAP_WAPI_WRITE_CONTAINER'
      EXPORTING
        WORKITEM_ID      = LV_WI
        DO_COMMIT        = SPACE
      IMPORTING
        RETURN_CODE      = LV_RETCODE
      TABLES
        SIMPLE_CONTAINER = LT_CONT
        MESSAGE_LINES    = LT_MESS.

*    IF NOT lv_retcode IS INITIAL OR
*       lt_cont IS INITIAL.
*
*      return-type = 'E'.
*      return-message = text-e12.
*      return-message_v4 = dp_list-docid.
*
*      APPEND return.
*      CLEAR return.
*
*      CALL FUNCTION 'DEQUEUE_/OPT/DOCID'
*        EXPORTING
*          mode_/opt/vim_1head = 'E'
*          mandt               = sy-mandt
*          docid               = dp_list-docid.
*
*      ROLLBACK WORK.
*      CONTINUE.
*
*    ENDIF.

******************************************************************
* Update the DP document
******************************************************************

    CREATE OBJECT LO_INDEXER
      EXPORTING
        DOCID = DP_LIST-DOCID.

    LO_INDEXER->INDEX_HEADER-AFTCS_VALIDATION = AFTCS_VALIDATION.
    LO_INDEXER->INDEX_HEADER-CUSTOM_FIELD4 = CUSTOM_FIELD4.
    LO_INDEXER->INDEX_HEADER-USER_ID = SY-UNAME.

    GET TIME STAMP FIELD LO_INDEXER->INDEX_HEADER-TIME_STAMP.

    CALL METHOD LO_INDEXER->UPDATE1( ).


******************************************************************
* Complete the dialog workitem
******************************************************************

* Get the workitem id of the DP workflow

    REFRESH LT_TASKS.
    LT_TASKS-WI_RH_TASK = LV_DB_TASK.
    APPEND LT_TASKS.

    CLEAR LT_TASKS.

    CALL FUNCTION 'SAP_WAPI_WORKITEMS_TO_OBJECT'
      EXPORTING
        OBJTYPE         = '/OPT/V1001'
        OBJKEY          = LV_OBJ_KEY
        TOP_LEVEL_ITEMS = SPACE
      IMPORTING
        RETURN_CODE     = LV_RETCODE
      TABLES
        TASK_FILTER     = LT_TASKS
        WORKLIST        = LT_ACTIVE_WF.

    READ TABLE LT_ACTIVE_WF INDEX 1.
    IF SY-SUBRC NE 0.
      RETURN-TYPE = 'E'.
      RETURN-MESSAGE = TEXT-E12.
      RETURN-MESSAGE_V4 = DP_LIST-DOCID.

      APPEND RETURN.
      CLEAR RETURN.

      CALL FUNCTION 'DEQUEUE_/OPT/DOCID'
        EXPORTING
          MODE_/OPT/VIM_1HEAD = 'E'
          MANDT               = SY-MANDT
          DOCID               = DP_LIST-DOCID.

      ROLLBACK WORK.
      CONTINUE.

    ENDIF.

    LOOP AT LT_ACTIVE_WF
    WHERE WI_STAT EQ 'READY' OR WI_STAT EQ 'STARTED'.

      CALL FUNCTION 'SWW_WI_ADMIN_COMPLETE'
        EXPORTING
          WI_ID                       = LT_ACTIVE_WF-WI_ID
          DO_COMMIT                   = SPACE
        EXCEPTIONS
          UPDATE_FAILED               = 1
          NO_AUTHORIZATION            = 2
          INFEASIBLE_STATE_TRANSITION = 3
          OTHERS                      = 4.
      IF SY-SUBRC <> 0.
        RETURN-TYPE = 'E'.
        RETURN-MESSAGE = TEXT-E12.
        RETURN-MESSAGE_V4 = DP_LIST-DOCID.

        APPEND RETURN.
        CLEAR RETURN.

        CALL FUNCTION 'DEQUEUE_/OPT/DOCID'
          EXPORTING
            MODE_/OPT/VIM_1HEAD = 'E'
            MANDT               = SY-MANDT
            DOCID               = DP_LIST-DOCID.

        ROLLBACK WORK.
        CONTINUE.

      ENDIF.

    ENDLOOP.
******************************************************************
* Release the lock
******************************************************************
*Begin of changes VIMI-4585 NKUSUMA
    CALL FUNCTION 'DEQUEUE_/OPT/DOCID'
      EXPORTING
        MODE_/OPT/VIM_1HEAD = 'E'
        MANDT               = SY-MANDT
        DOCID               = DP_LIST-DOCID.
    CONTINUE.
*End of changes VIMI-4585 NKUSUMA
  ENDLOOP.
ENDFUNCTION.
